package io.renren.controller;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.renren.entity.InterviewEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.entity.RecruitdamandEntity;
import io.renren.service.RecruitdamandService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-06-24 00:22:25
 */
@RestController
@RequestMapping("recruitdamand")
public class RecruitdamandController {
	@Autowired
	private RecruitdamandService recruitdamandService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("recruitdamand:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		recruitdamandService.syncRecruitDamandFormB1(params);
		List<RecruitdamandEntity> recruitdamandList = recruitdamandService.queryList(query);
		int total = recruitdamandService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(recruitdamandList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("recruitdamand:info")
	public R info(@PathVariable("id") Integer id){
		RecruitdamandEntity recruitdamand = recruitdamandService.queryObject(id);
		
		return R.ok().put("recruitdamand", recruitdamand);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("recruitdamand:save")
	public R save(@RequestBody RecruitdamandEntity recruitdamand){
		recruitdamandService.save(recruitdamand);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("recruitdamand:update")
	public R update(@RequestBody RecruitdamandEntity recruitdamand){
		recruitdamandService.update(recruitdamand);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("recruitdamand:delete")
	public R delete(@RequestBody Integer[] ids){
		recruitdamandService.deleteBatch(ids);
		
		return R.ok();
	}

	@RequestMapping("/choose")
	@RequiresPermissions("recruitdamand:list")
	public R choosePerson(@RequestParam Map<String, Object> params){
		/*Set<String> set = params.keySet();
		Iterator<String> it = set.iterator();
		while(it.hasNext()){
			String key = it.next();
			System.out.println(key + ":" + params.get(key));
		}*/
		String idss = params.get("ids").toString();
		JSONArray ids = JSONArray.parseArray(idss);
		List<Map<String, Object>> result = recruitdamandService.choosePerson(ids);
		PageUtils pageUtil = new PageUtils(result, result.size(), 100, 1);

		return R.ok().put("page", pageUtil);
	}

	@RequestMapping("/selected")
	@RequiresPermissions("recruitdamand:save")
	public R selected(@RequestBody JSONArray params,HttpServletRequest request){

		int i = recruitdamandService.selectedEmp(params,request.getParameter("id"));
		if(i == 0){
			return R.error();
		}else{
			return R.ok();
		}
	}
	
}
